// pages/goods/list.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentItem: '',
    currentBrand: '',
    goodsType: '',
    hiddenLoading: false,
    goodsList: [],
    perPage: 10,
    curPage: 1,
    scrollHeight: 0,
    allowScroll: true,
    topNum: 0,
    hiddenTop: true,
    loadingComplete: false,
    curType: 0, //筛选类型
    order: 'id',
    orderType: 'desc'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //初始化
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight - 45
        });
      }
    });
    var list_id = '';
    var brand_id = '';
    var goods_type = '';
    if (typeof (options.id) != "undefined") {
      list_id = options.id;
    }
    if (typeof (options.brand_id) != "undefined") {
      brand_id = options.brand_id;
    }
    if (typeof (options.type) != "undefined") {
      goods_type = options.type;
    }
    that.setGoodsListData(list_id, brand_id, goods_type);
    that.setCategoryData(list_id, brand_id, goods_type);
  },

  //下拉刷新
  onPullDownRefresh: function () {
    var that = this;
    that.setGoodsListData(that.data.currentItem, that.data.currentBrand, that.data.goodsType);
    that.setCategoryData(that.data.currentItem, that.data.currentBrand, that.data.goodsType);
    wx.stopPullDownRefresh();
  },

  //滚动到底部触发事件  
  showScrollLower: function () {
    var that = this;
    if (that.data.allowScroll) {
      that.setData({
        allowScroll: false
      });
      that.setGoodsListData(that.data.currentItem, that.data.currentBrand, that.data.goodsType);
    }
  },

  // 获取滚动条当前位置
  scrolltoupper: function (e) {
    if (e.detail.scrollTop > 100) {
      this.setData({
        hiddenTop: false
      });
    } else {
      this.setData({
        hiddenTop: true
      });
    }
  },

  //回到顶部
  goTop: function () { // 一键回到顶部
    this.setData({
      topNum: 0
    });
  },

  //初始化当前分类
  setCategoryData: function (id, brand_id, goods_type) {
    var that = this;
    var m_title = '';
    if (goods_type != '') {
      switch (goods_type) {
        case 'top':
          m_title = "人气推荐";
          break;
        case 'free':
          m_title = "免邮商品";
          break;
        case 'new':
          m_title = "新品首发";
          break;
        case 'hot':
          m_title = "热门推荐";
          break;
        case 'vip':
          m_title = "VIP推荐";
          break;
      }
      wx.setNavigationBarTitle({
        title: m_title
      })
    } else {
      var m_url = rootDocment + '/api_goods_category/' + id;
      var paraArr = new Array();
      paraArr['id'] = id;
      if (brand_id != '') {
        var m_url = rootDocment + '/api_brand/' + brand_id;
        paraArr['id'] = brand_id;
      }
      var sign = app.signature(paraArr);
      wx.request({
        url: m_url,
        data: {
          sign: sign
        },
        method: 'GET',
        header: {},
        success: function (res) {
          console.log(res.data);
          if (brand_id != '') {
            m_title = res.data['title'];
          } else {
            m_title = res.data['wap_title'];
          }
          wx.setNavigationBarTitle({
            title: m_title
          })
        }
      })
    }

  },

  //初始商品列表
  setGoodsListData: function (id, brand_id, goods_type) {
    var that = this;
    if (!that.data.loadingComplete) {
      var paraArr = new Array();
      paraArr['sort'] = id;
      paraArr['size'] = that.data.perPage;
      paraArr['page'] = that.data.curPage;
      paraArr['order'] = that.data.order;
      paraArr['order_type'] = that.data.orderType;
      paraArr['brand_id'] = brand_id;
      paraArr['type'] = goods_type;
      var sign = app.signature(paraArr);
      wx.request({
        url: rootDocment + '/api_goods',
        data: {
          sort: id,
          brand_id: paraArr['brand_id'],
          size: paraArr['size'],
          page: paraArr['page'],
          order: paraArr['order'],
          order_type: paraArr['order_type'],
          type: paraArr['type'],
          sign: sign
        },
        method: 'GET',
        header: {},
        success: function (res) {
          that.setData({
            currentItem: id,
            currentBrand: brand_id,
            goodsType: goods_type,
            hiddenLoading: true
          });
          if (res.data.data.length > 0) { //如果有数据
            var list = that.data.goodsList;
            for (var i = 0; i < res.data.data.length; i++) {
              list.push(res.data.data[i]);
            }
            that.setData({
              goodsList: list
            });
            if (res.data.data.length == that.data.perPage) {
              that.setData({
                curPage: that.data.curPage + 1,
                allowScroll: true
              });
            } else {
              that.setData({
                loadingComplete: true
              });
            }
          } else {
            that.setData({
              loadingComplete: true
            });
          }

        }
      })
    }
  },

  //筛选
  screenGoods: function (e) {
    var that = this;
    var m_type = e.currentTarget.dataset.type;
    var m_order = 'id';
    var m_orderType = 'desc';
    if (m_type > 0) {
      if (that.data.orderType == 'desc') {
        m_orderType = 'asc'
      }
    }
    if (m_type == 1) m_order = 'price';
    if (m_type == 2) m_order = 'sales';
    that.setData({
      curType: m_type,
      order: m_order,
      orderType: m_orderType,
      curPage: 1,
      goodsList: [],
      hiddenLoading: false,
      loadingComplete: false
    });
    that.setGoodsListData(that.data.currentItem, that.data.currentBrand, that.data.goodsType);
  },

})